const request=require('request')
const path=require('path')
const config=require('./config')
const analyze=require('./analyze')
const fs=require('fs')



function start() {
  request(config.url,function (err, res, body) {
    console.log('start');
    if(!err && res){
      console.log('start');
      analyze.findImg(body,downLoad)
    }
  })
}
function downLoad(imgUrl,i) {
  // let ext=imgUrl.split('.').pop();

  if(imgUrl && (imgUrl.indexOf('http:')===-1) && (imgUrl.indexOf('data:image')===-1)){
    imgUrl='http:'+imgUrl
  }
  console.log("imgUrl:",imgUrl);
  request(imgUrl).pipe(fs.createWriteStream(path.join(config.imgDir,i+'.'+'png'),{
    'encoding':'utf8'
  }))
  console.log(i)
}

start()
